function main() {
    const self = this;
    // self.isLoading = true;
    debugger
    const formPartEle = self.$$root().$$("form_part_87u0zh");
    const formRenderEle = formPartEle && formPartEle.$$getRenderElement();
    const humanId = formRenderEle && formRenderEle.getDocumentNo();

    if (!formPartEle || !formRenderEle) {
        console.warn("表单元素未找到");
        self.isLoading = false;
        return;
    }

    const operation = humanId ? handleEdit : handleCreate;
    operation(self, formPartEle, humanId)
        .then(function () {
            self.isLoading = false;
        })
        .catch(function (error) {
            console.error("操作失败: " + (error.message || "未知错误"));
            console.error((humanId ? "编辑" : "新增") + "操作出错:", error);
            self.isLoading = false;
        });
}

/**
 * 编辑人员信息
 */
function handleEdit(self, formRenderEle, humanId) {
    return formRenderEle.$$getRenderElement().onSave()
        .then(function (res) {
            if (!res || !res.result) throw new Error("保存失败，无返回结果");
            console.log("提交成功");
            let valid_flag = res.result.t_2488d76f089b4f00aae44062307659fd.number_input_d0ez1e;
            if (valid_flag == "1") {
                syncHumanToUserCenter(humanId, self);
            }
            return goBackPage(self);
        })
        .then(function () {
            return true;
            // return syncHumanToUserCenter(humanId, self);
        });
}

/**
 * 新增人员信息
 */
function handleCreate(self, formPartEle) {
    return checkApprovalRequirement()
        .then(function (needsApproval) {
            return needsApproval
                ? handleCreateWithApproval(self, formPartEle)
                : handleDirectCreate(self, formPartEle);
        });
}

/**
 * 需要审批的新增
 */
function handleCreateWithApproval(self, formPartEle) {
    return setApprovalValues(self)
        .then(function () {
            return saveHuman(self, formPartEle);
        })
        .then(function (res) {
            validateResponse(res, "保存失败，无返回结果");
            const humanUUID = res.result["t_2488d76f089b4f00aae44062307659fd"].text_input_1cr5k1;
            // const documentNo = res.result.documentNo;
            // self.$message.success("提交成功，等待审批");
            console.log("提交成功，等待审批")
            return Promise.all([
                insertApproval(humanUUID),
                // syncHumanToUserCenter([{ name: "id", valueContent: documentNo }]);
            ]);
        })
        .then(function () {
            return goBackPage(self);
        });
}

/**
 * 无需审批的新增
 */
function handleDirectCreate(self, formPartEle) {
    return setDirectValues(self)
        .then(function () {
            return saveHuman(self, formPartEle);
        })
        .then(function (res) {
            validateResponse(res, "保存失败，无返回结果");
            const documentNo = res.result.documentNo;
            console.log("新增成功");

            return syncHumanToUserCenter([{ name: "id", valueContent: documentNo }],self);
        })
        .then(function () {
            return goBackPage(self); // 跳转到上一页
        });
}

/**
 * 检查是否需要审批
 */
function checkApprovalRequirement() {
    var formula = "ENTITY_SELECT_ONE('t_4916fdfaf0174ad19ff7e4ed5355c90a',enable_flag,QUERY_EQ(id,1))";
    return api.EXEC_FORMULA(formula)
        .then(function (response) {
            return response.result === "1";
        });
}

/**
 * 设置需要审批的默认值
 */
function setApprovalValues(self) {
    var setValues = [
        self.$$("number_input_d0ez1e").$$setValue("0"),  // 待审核状态
        self.$$("single_select_f97zsd").$$setValue("5")  // 待入职
    ];
    return Promise.all(setValues);
}

/**
 * 设置无需审批的默认值
 */
function setDirectValues(self) {
    var setValues = [
        self.$$("number_input_d0ez1e").$$setValue("1"),  // 在职状态
        self.$$("single_select_f97zsd").$$setValue("1")  // 已入职
    ];
    return Promise.all(setValues);
}

/**
 * 保存人员信息
 */
function saveHuman(self, formPartEle) {
    return formPartEle.$$getRenderElement().onSave();
}


/**
 * 同步到用户中心
 */
function syncHumanToUserCenter(params,self) {
    return api.DATA_SOURCE_EXECUTE(
        "ds_api:7a33e6eb-1ea6-46d3-be49-afca3a903a58",
        "api",
        params,
        [],
        ""
    ).then(function (res) {
        var result = JSON.parse(res.result);
        console.log("同步用户中心成功:", result);
        return result;
    }).catch(function (error) {
        console.error("同步用户中心失败:", error);
        goBackPage(self);
        throw error;
    });
}

/**
 * 创建审批记录
 */
function insertApproval(humanUUID) {
    var sql = "INSERT('t_655b23a930504145bf2ba2f014780dc6'," +
        "['human_uuid','" + humanUUID + "'," +
        "'approval_type_code',1," +
        "'approval_type_name','人员新增'," +
        "'latest_flag','1'," +
        "'approval_result', '0'," +
        "'create_human_id',USER_ID()," +
        "'create_time',NOW()])";

    return api.EXEC_FORMULA(sql)
        .then(function (res) {
            console.log("新增人员审核成功:", res);
            return res;
        })
        .catch(function (error) {
            console.error("新增人员审核失败:", error);
            throw error;
        });
}

/**
 * 验证API响应
 */
function validateResponse(res, errorMessage) {
    if (!res || !res.result) {
        throw new Error(errorMessage);
    }
}

/**
 * 返回上一页
 */
function goBackPage(self) {
    const pageRenderElement = self.$$root().$parent.$parent.$parent;
    return pageRenderElement.goHistory(-1);
}
